package com.cardmanager.pro.business.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;

import com.cardmanager.pro.pojo.CreditCardAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cardmanager.pro.business.ConsumeTaskPOJOBusiness;
import com.cardmanager.pro.pojo.ConsumeTaskPOJO;
import com.cardmanager.pro.pojo.RepaymentTaskPOJO;
import com.cardmanager.pro.repository.ConsumeTaskPOJORepository;
import com.cardmanager.pro.repository.RepaymentTaskPOJORepository;

import cn.jh.common.utils.DateUtil;

@Service
public class ConsumeTaskPOJOBusinessImpl implements ConsumeTaskPOJOBusiness {
	@Autowired
	private ConsumeTaskPOJORepository consumeTaskPOJORepository;

	@Autowired
	private RepaymentTaskPOJORepository repaymentTaskPOJORepository;

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Autowired
	private EntityManager em;

	@Transactional
	@Override
	public ConsumeTaskPOJO save(ConsumeTaskPOJO consumeTaskPOJO) {
		ConsumeTaskPOJO model = consumeTaskPOJORepository.save(consumeTaskPOJO);
		em.flush();
		em.clear();
		return model;
	}

	@Override
	public int queryTaskStatus0Count(String userIdStr, String creditCardNumber, int taskStatus) {
		return consumeTaskPOJORepository.queryTaskStatusCount(userIdStr, creditCardNumber, taskStatus);
	}

	/**
	 * 查询当天和当天之前的历史记录表
	 * @return
	 */
	@Override
	public List<ConsumeTaskPOJO> findTaskType2AndTaskStatus0RepaymentTask() {
		Date nowDate = new Date();
		String nowDateTime = DateUtil.getDateStringConvert(new String(), nowDate, "yyyy-MM-dd HH:mm:ss");
		String nowDateString = DateUtil.getDateStringConvert(new String(), nowDate, "yyyy-MM-dd");
		//获取小等于当前时间等待执行的任务
		List<ConsumeTaskPOJO> list = consumeTaskPOJORepository.findTaskTypeAndTaskStatusRepaymentTask(2, 0, nowDateTime, nowDateString);
		if(list.size() > 0 ){
			for(ConsumeTaskPOJO pojo : list){
				pojo.setTaskStatus(6);
				//更新待执行状态的任务，避免二次定时任务重复消费或还款；
				consumeTaskPOJORepository.save(pojo);
			}
		}
		return list;
	}

	@Override
	public int queryTaskStatus0AndTaskType0Count(String userId, String creditCardNumber, int taskStatus, int taskType) {
		return consumeTaskPOJORepository.queryTaskStatusAndTaskTypeCount(userId, creditCardNumber, taskStatus,
				taskType);
	}

	public int queryTaskStatus(String userId, String creditCardNumber) {
		String sql = "select * from t_consume_task where user_id = " + userId + " and credit_card_number = " + creditCardNumber + " and task_status = 0  and task_type = 2 ";
		List<ConsumeTaskPOJO> list = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(ConsumeTaskPOJO.class));
		int count = 0;
		if(list.size() > 0){
			count = 1;
		}
		return count;
	}

	/**
	 * 查询待完成任务
	 * @param userId
	 * @param creditCardNumber
	 * @return
	 */
	public List<RepaymentTaskPOJO> queryRepaymentTask(String userId, String creditCardNumber) {
		String sql = "select * from t_repayment_task where user_id = '" + userId + "' and credit_card_number = '" + creditCardNumber + "' and task_status in (0,5) ";
		List<RepaymentTaskPOJO> list = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(RepaymentTaskPOJO.class));
		return list;
	}

	@Transactional
	public void updateRepaymentTask(String userId, String creditCardNumber) {
		String sql = "update t_repayment_task set task_status = 0,order_status = 1 where user_id = '" + userId + "' and credit_card_number = '" + creditCardNumber +"' ";
		jdbcTemplate.update(sql);
	}

	@Transactional
	public void deleteRepaymentTask(List<RepaymentTaskPOJO> list){
		for(RepaymentTaskPOJO pojo : list){
			repaymentTaskPOJORepository.delete(Long.valueOf(pojo.getId()));
		}
	}

	@Override
	public int queryOrderStatus1AndTaskType0Count(String userId, String creditCardNumber, int OrderStatus,
			int TaskType) {
		return consumeTaskPOJORepository.queryOrderStatusAndTaskTypeCount(userId, creditCardNumber, OrderStatus,
				TaskType);
	}

	@Transactional
	@Override
	public List<ConsumeTaskPOJO> saveArrayList(List<ConsumeTaskPOJO> consumeTaskPOJOs) {
		for (int i = 0; i < consumeTaskPOJOs.size(); i++) {
			consumeTaskPOJOs.set(i, consumeTaskPOJORepository.save(consumeTaskPOJOs.get(i)));
		}
		return consumeTaskPOJOs;
	}

	@Override
	public List<ConsumeTaskPOJO> findByRepaymentTaskId(String repaymentTaskId) {
		List<ConsumeTaskPOJO> models = consumeTaskPOJORepository.findByRepaymentTaskId(repaymentTaskId);
		if(models != null && models.size() > 0){
			for(ConsumeTaskPOJO model:models){
				model.setCreateTime(model.getCreateTime().substring(0, model.getCreateTime().lastIndexOf(".")));
				model.setExecuteDateTime(model.getExecuteDateTime().substring(0, model.getExecuteDateTime().lastIndexOf(".")));
			}
		}
		return models;
	}

	@Override
	public ConsumeTaskPOJO findByOrderCode(String orderCode) {
		return consumeTaskPOJORepository.findByOrderCode(orderCode);
	}

	// 不缓存

	/**
	 * 更新消费任务状态
	 * @param repaymentTaskId
	 * @param returnMessage
	 */
	@Transactional
	@Override
	public void updateTaskStatus4AndReturnMessageByRepaymentTaskId(String repaymentTaskId, String returnMessage) {
		List<ConsumeTaskPOJO> consumeTaskPOJOs = consumeTaskPOJORepository.findByRepaymentTaskId(repaymentTaskId);
		if (consumeTaskPOJOs != null && consumeTaskPOJOs.size() > 0) {
			String consumeTaskId;
			for (ConsumeTaskPOJO consumeTaskPOJO : consumeTaskPOJOs) {
				consumeTaskId = consumeTaskPOJO.getConsumeTaskId();
				if (!"2".equals(consumeTaskId.substring(consumeTaskId.length() - 1))) {
					consumeTaskPOJO.setRealAmount(BigDecimal.ZERO);
					consumeTaskPOJO.setTaskStatus(4);
					consumeTaskPOJO.setReturnMessage(returnMessage);
					consumeTaskPOJORepository.save(consumeTaskPOJO);
				}
			}
		}
		RepaymentTaskPOJO repaymentTaskPOJO = repaymentTaskPOJORepository.findByRepaymentTaskId(repaymentTaskId);
		if (repaymentTaskPOJO != null) {
			repaymentTaskPOJO.setRealAmount(BigDecimal.ZERO);
			repaymentTaskPOJO.setTaskStatus(4);
			repaymentTaskPOJO.setReturnMessage(returnMessage);
			repaymentTaskPOJO = repaymentTaskPOJORepository.save(repaymentTaskPOJO);
		}
	}

	@Transactional
	@Override
	public void saveArrayListTaskAll(List<ConsumeTaskPOJO> consumeTaskPOJOs, RepaymentTaskPOJO[] repaymentTaskPOJOs) {
		for (ConsumeTaskPOJO consumeTaskPOJO : consumeTaskPOJOs) {
			consumeTaskPOJORepository.save(consumeTaskPOJO);
		}
		for (RepaymentTaskPOJO repaymentTaskPOJO : repaymentTaskPOJOs) {
			repaymentTaskPOJORepository.save(repaymentTaskPOJO);
		}
	}

	@Override
	public int queryOrderStatus1AndTaskType0Count(String userId, String creditCardNumber) {
		return consumeTaskPOJORepository.queryOrderStatusAndTaskTypeCount(userId, creditCardNumber, 1, 0);
	}

	@Override
	public ConsumeTaskPOJO findByTaskType0AndTaskStatus0ConsumeTaskPOJO(String userId, String creditCardNumber) {
		return consumeTaskPOJORepository.findByTaskTypeAndTaskStatusConsumeTaskPOJO(userId, creditCardNumber, 0, 0);
	}

	@Transactional
	@Override
	public void deleteRepaymentTaskAndConsumeTask(RepaymentTaskPOJO repaymentTaskPOJO,
			List<ConsumeTaskPOJO> consumeTaskPOJOs) {
		for (ConsumeTaskPOJO consumeTaskPOJO : consumeTaskPOJOs) {
			consumeTaskPOJORepository.save(consumeTaskPOJO);
		}
		repaymentTaskPOJORepository.save(repaymentTaskPOJO);
	}

	@Override
	public ConsumeTaskPOJO findByConsumeTaskId(String consumeTaskId) {
		return consumeTaskPOJORepository.findByConsumeTaskId(consumeTaskId);
	}
}
